package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.util.Filter;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSModel;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Iscream"}, name="PrisonPete", version=1.5D)
public class Prison extends Random
{
  private static final int PRISON_MATE = 3118;
  private static final int LEVER_ID = 10817;
  private static final int DOOR_KEY = 6966;
  private int unlocked;
  private int state;
  private RSNPC balloonToPop;
  private RSNPC pete;
  private boolean talkedtopete;
  private boolean key;
  private boolean lucky;

  public Prison()
  {
    this.state = 0;

    this.talkedtopete = false;
    this.key = false;
    this.lucky = false;
  }

  public boolean activateCondition()
  {
    if (this.game.isLoggedIn()) {
      this.pete = this.npcs.getNearest(new String[] { "Prison Pete" });
      if (this.pete != null) {
        return this.objects.getNearest(new int[] { 10817 }) != null;
      }
    }
    return false;
  }

  public int loop()
  {
    if (this.npcs.getNearest(new String[] { "Prison Pete" }) == null) {
      return -1;
    }
    if (!this.talkedtopete) {
      this.camera.setPitch(true);
      if ((this.camera.getAngle() < 175) || (this.camera.getAngle() > 185)) {
        this.camera.setAngle(random(175, 185));
        return random(500, 750);
      }
    }
    RSObject localRSObject1;
    switch (this.state) {
    case 0:
      this.pete = this.npcs.getNearest(new String[] { "Prison Pete" });
      if (interfaceContains("Lucky you!")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.lucky = true;
        return random(500, 600);
      }
      if (interfaceContains("should leave")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.unlocked = 10;
        return random(500, 600);
      }
      if (this.inventory.getCount(false) == 28) if (!this.inventory.containsAll(new int[] { 6966 }))
        {
          log("Not enough space for this random. Depositing an Item");
          localRSObject1 = this.objects.getNearest(new int[] { 32924 });
          if (localRSObject1 != null) {
            if (!this.calc.tileOnScreen(localRSObject1.getLocation())) {
              if (!this.walking.walkTileMM(localRSObject1.getLocation().randomize(3, 3))) {
                this.walking.getPath(localRSObject1.getLocation().randomize(3, 3)).traverse();
                return random(500, 700);
              }
              return random(1000, 1500);
            }
            this.camera.turnToObject(localRSObject1, 20);
            if (localRSObject1.doAction("Deposit")) {
              sleep(random(1800, 2000));
              if (getMyPlayer().isMoving()) {
                sleep(random(200, 500));
              }
              if (this.interfaces.get(11).isValid()) {
                sleep(random(700, 1200));
                this.interfaces.get(11).getComponent(17).getComponent(random(16, 17)).doAction("Dep");
                sleep(random(700, 1200));
                this.interfaces.getComponent(11, 15).doClick();
              }
              return random(400, 500);
            }
            return random(500, 800);
          }
          return random(500, 600);
        }

      if (getMyPlayer().isMoving()) {
        return random(250, 500);
      }
      if (interfaceContains("minute")) {
        this.talkedtopete = true;
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
          return random(500, 600);
        }
        return random(500, 600);
      }

      if ((this.interfaces.get(228).isValid()) && (this.interfaces.get(228).containsText("How do")))
      {
        this.interfaces.get(228).getComponent(3).doClick();
        return random(500, 600);
      }
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
        return random(1000, 1200);
      }
      if ((!this.talkedtopete) && (this.pete != null) && (!this.interfaces.get(228).isValid()) && (!this.interfaces.canContinue()))
      {
        if (!this.calc.tileOnScreen(this.pete.getLocation())) {
          this.walking.walkTileMM(this.pete.getLocation());
          return random(1000, 1400);
        }
        if (this.pete.doAction("talk")) {
          return random(1500, 1600);
        }
        this.camera.turnToTile(this.pete.getLocation());
        return random(500, 600);
      }

      if (this.unlocked == 3) {
        this.state = 4;
        return random(250, 500);
      }
      if ((this.unlocked <= 2) && (this.talkedtopete)) {
        this.state = 1;
        return random(500, 600);
      }
      return random(350, 400);
    case 1:
      if (interfaceContains("Lucky you!")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.lucky = true;
        return random(500, 600);
      }
      if (interfaceContains("should leave")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.unlocked = 10;
        return random(500, 600);
      }
      if ((this.interfaces.get(273).getComponent(3).isValid()) && 
        (atLever())) {
        if ((this.balloonToPop != null) && (this.interfaces.get(273).getComponent(4).doAction("Close"))) {
          this.state = 2;
          return random(800, 900);
        }
        return random(500, 700);
      }

      localRSObject1 = this.objects.getNearest(new int[] { 10817 });
      if ((localRSObject1 != null) && (this.talkedtopete)) {
        if (!this.calc.tileOnScreen(localRSObject1.getLocation())) {
          this.walking.walkTileMM(localRSObject1.getLocation());
          return random(1000, 1200);
        }
        if ((!getMyPlayer().isMoving()) && (this.calc.tileOnScreen(localRSObject1.getLocation())))
        {
          if (localRSObject1.doAction("Pull")) {
            sleep(random(1400, 1600));
            if (atLever()) {
              if ((this.balloonToPop != null) && (this.interfaces.get(273).getComponent(4).doAction("Close"))) {
                this.state = 2;
                return random(800, 900);
              }
              return random(500, 700);
            }
            return random(500, 600);
          }
          this.camera.turnToTile(localRSObject1.getLocation());
          return random(500, 600);
        }
      }

      if (!this.talkedtopete) {
        this.state = 0;
        return random(500, 600);
      }
      return random(500, 600);
    case 2:
      if (interfaceContains("Lucky you!")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.lucky = true;
        return random(500, 600);
      }
      if (interfaceContains("should leave")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.unlocked = 10;
        return random(500, 600);
      }
      if (getMyPlayer().isMoving()) {
        return random(250, 500);
      }
      if ((this.balloonToPop == null) && (this.unlocked <= 2)) {
        this.state = 1;
        return random(500, 700);
      }
      if (this.unlocked == 3) {
        this.state = 4;
      }

      if (!this.inventory.containsAll(new int[] { 6966 })) {
        if (this.calc.tileOnScreen(this.balloonToPop.getLocation())) {
          this.balloonToPop.doAction("Pop");
          return random(1200, 1400);
        }
        if (!getMyPlayer().isMoving()) {
          this.walking.walkTileMM(this.balloonToPop.getLocation().randomize(2, 2));
          return random(500, 750);
        }
        return random(500, 750);
      }

      if (this.inventory.containsAll(new int[] { 6966 })) {
        this.key = false;
        this.state = 3;
        return random(500, 700);
      }
      return random(350, 400);
    case 3:
      this.pete = this.npcs.getNearest(new String[] { "Prison Pete" });
      if (getMyPlayer().isMoving()) {
        return random(250, 500);
      }
      if (interfaceContains("Lucky you!")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.lucky = true;
        return random(500, 600);
      }
      if (interfaceContains("should leave")) {
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
        }
        this.state = 4;
        this.unlocked = 10;
        return random(500, 600);
      }
      if (interfaceContains("you got all the keys")) {
        this.key = true;
        this.unlocked = 5;
        this.state = 4;
        this.balloonToPop = null;
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
          return random(500, 600);
        }
        return random(250, 500);
      }
      if (interfaceContains("Hooray")) {
        this.key = true;
        if (this.interfaces.canContinue()) {
          this.interfaces.clickContinue();
          return random(500, 600);
        }
      }
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
        return random(500, 600);
      }
      if ((this.pete != null) && (!this.calc.tileOnScreen(this.pete.getLocation())) && (!this.interfaces.get(243).isValid())) {
        this.walking.walkTileMM(this.pete.getLocation());
        return random(400, 600);
      }
      if (!this.inventory.containsAll(new int[] { 6966 })) if ((this.npcs.getNearest(new int[] { 3118 }) != null) && (this.unlocked <= 2) && (this.key))
        {
          this.unlocked += 1;
          this.state = 0;
          this.balloonToPop = null;
          return random(350, 400);
        }

      if ((this.inventory.containsAll(new int[] { 6966 })) && (!getMyPlayer().isMoving())) {
        this.inventory.getItem(new int[] { 6966 }).doAction("Return");
        return random(1000, 2000);
      }
      if (!this.inventory.containsAll(new int[] { 6966 })) if ((this.npcs.getNearest(new int[] { 3118 }) != null) && (this.unlocked <= 2) && (!this.key))
        {
          this.state = 0;
          this.balloonToPop = null;
          return random(350, 400);
        }

      return random(350, 400);
    case 4:
      RSTile localRSTile = new RSTile(2086, 4459);
      if ((this.unlocked <= 2) && (!this.lucky)) {
        this.state = 0;
        return random(500, 600);
      }
      if (!this.calc.tileOnScreen(localRSTile)) {
        this.walking.walkTileMM(localRSTile);
        return random(400, 500);
      }
      if (this.calc.tileOnScreen(localRSTile)) {
        RSObject localRSObject2 = this.objects.getNearest(new int[] { 11177, 11178 });
        if (localRSObject2 != null) {
          localRSObject2.doAction("Open");
        }
        return random(500, 600);
      }
      return random(200, 400);
    }
    return random(200, 400);
  }

  public void onFinish()
  {
    if (this.lucky) {
      this.log.info("Failed to complete Prison Pete. Stopping now.");
      sleep(5000, 10000);
      stopScript(false);
    }
  }

  public short[] setItemIDs(int paramInt)
  {
    switch (paramInt) {
    case 10749:
      return Balloons.SKINNY_BENT_TAIL;
    case 10750:
      return Balloons.SKINNY_NORMAL_TAIL;
    case 10751:
      return Balloons.FATTY;
    case 10752:
      return Balloons.HORNY;
    }
    return new short[0];
  }

  public boolean interfaceContains(String paramString) {
    RSInterface[] arrayOfRSInterface1 = this.interfaces.getAll();
    for (RSInterface localRSInterface : arrayOfRSInterface1) {
      if (localRSInterface != null) {
        int k = localRSInterface.getComponents().length;
        for (int m = 0; m < k; m++) {
          if ((localRSInterface.getComponent(m).getText() != null) && (localRSInterface.getComponent(m).getText().contains(paramString))) {
            return true;
          }
        }
      }
    }
    return false;
  }

  public boolean atLever() {
    if (this.interfaces.get(273).getComponent(3).isValid()) {
      Filter localFilter = RSModel.newVertexFilter(setItemIDs(this.interfaces.get(273).getComponent(3).getModelID()));
      this.balloonToPop = this.npcs.getNearest(new Filter(localFilter) {
        public boolean accept(RSNPC paramRSNPC) {
          return this.val$filter.accept(paramRSNPC.getModel());
        }
      });
      if (this.balloonToPop != null) {
        return true;
      }
    }
    return false;
  }

  private static class Balloons
  {
    static final short[] FATTY = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 2, 2, 8, 8, 5, 5, 9, 9, 10, 11, 11, 15, 16, 17, 13, 14, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 28, 29, 29, 30, 30, 31, 32, 33, 33, 36, 36, 36, 36, 36, 37, 37, 37, 38, 38, 39, 39, 40, 40, 41, 42, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 48, 48, 49, 49, 50, 51, 51, 52, 52, 47, 47, 53, 53, 50, 50, 54, 54, 55, 22, 22, 59, 60, 61, 57, 58, 64, 64, 64, 64, 64, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 67, 67, 68, 68, 43, 69, 69, 70, 70, 66, 66, 71, 71, 43, 43, 72, 72, 73, 74, 74, 78, 79, 80, 76, 77, 83, 83, 83, 83, 83, 84, 84, 84, 85, 85, 86, 86, 87, 87, 89, 90, 92, 92, 92, 92, 93, 93, 94, 94, 95, 5, 98, 98, 98, 98, 98, 99, 99, 99, 22, 22, 100, 100, 101, 101, 101, 102, 102, 103, 103, 103, 104, 105, 106, 106, 107, 107, 108, 109, 110, 110, 113, 113, 113, 113, 113, 114, 114, 114, 22, 22, 115, 115, 116, 116, 116, 117, 117, 118, 118, 118, 119, 120, 121, 121, 122, 122, 123, 124, 125, 125, 128, 128, 128, 128, 128, 129, 129, 129, 22, 22, 130, 130, 131, 131, 131, 132, 132, 133, 133, 133, 134, 135, 136, 136, 137, 137, 138, 32, 139, 139 };
    static final short[] HORNY = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 2, 2, 8, 8, 5, 5, 9, 9, 10, 11, 11, 15, 16, 17, 13, 14, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 28, 29, 29, 30, 30, 31, 32, 33, 33, 36, 36, 36, 36, 36, 37, 37, 37, 38, 38, 39, 39, 40, 40, 22, 41, 41, 42, 43, 44, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 50, 50, 51, 51, 52, 53, 53, 54, 54, 49, 49, 55, 55, 52, 52, 56, 56, 57, 22, 22, 61, 62, 63, 59, 60, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 69, 69, 70, 70, 43, 71, 71, 72, 72, 68, 68, 73, 73, 43, 43, 74, 74, 75, 76, 76, 80, 81, 82, 78, 79, 85, 85, 85, 85, 85, 86, 86, 86, 87, 87, 88, 88, 89, 89, 90, 91, 91, 92, 43, 93, 96, 96, 96, 96, 97, 97, 98, 98, 99, 5, 102, 102, 102, 102, 102, 102, 103, 103, 103, 103, 104, 104, 105, 105, 106, 106, 106, 107, 107, 108, 109, 109, 110, 110, 110, 111, 112, 113, 117, 115, 119, 119, 119, 119, 119, 119, 120, 120, 120, 120, 121, 121, 122, 122, 123, 123, 123, 124, 124, 125, 126, 126, 127, 127, 127, 128, 129, 130, 134, 132, 136, 136, 136, 136, 136, 137, 137, 137, 22, 22, 138, 138, 139, 139, 139, 140, 140, 141, 141, 141, 142, 143, 144, 144, 145, 145, 146, 147, 148, 148, 151, 151, 151, 151, 151, 152, 152, 152, 22, 22, 153, 153, 154, 154, 154, 155, 155, 156, 156, 156, 157, 158, 159, 159, 160, 160, 161, 162, 163, 163 };
    static final short[] SKINNY_BENT_TAIL = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 2, 2, 8, 8, 5, 5, 9, 9, 10, 11, 11, 15, 16, 17, 13, 14, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 28, 29, 29, 30, 30, 31, 32, 33, 33, 36, 36, 36, 36, 36, 37, 37, 37, 38, 38, 39, 39, 40, 40, 22, 41, 41, 42, 43, 44, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 50, 50, 51, 51, 52, 53, 53, 54, 54, 49, 49, 55, 55, 52, 52, 56, 56, 57, 22, 22, 61, 62, 63, 59, 60, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 69, 69, 70, 70, 43, 71, 71, 72, 72, 68, 68, 73, 73, 43, 43, 74, 74, 75, 76, 76, 80, 81, 82, 78, 79, 85, 85, 85, 85, 85, 86, 86, 86, 87, 87, 88, 88, 89, 89, 90, 91, 91, 92, 43, 93, 96, 96, 96, 96, 96, 97, 97, 97, 98, 98, 99, 99, 100, 100, 5, 101, 101, 102, 103, 104, 107, 107, 107, 107, 107, 108, 108, 108, 109, 109, 110, 110, 111, 111, 103, 112, 112, 113, 114, 115 };
    static final short[] SKINNY_NORMAL_TAIL = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 2, 2, 8, 8, 5, 5, 9, 9, 10, 11, 11, 15, 16, 17, 13, 14, 20, 20, 20, 20, 20, 21, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 28, 29, 29, 30, 30, 31, 32, 33, 33, 36, 36, 36, 36, 37, 37, 38, 38, 39, 22, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 45, 45, 46, 46, 47, 48, 48, 49, 49, 44, 44, 50, 50, 47, 47, 51, 51, 52, 22, 22, 56, 57, 58, 54, 55, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 64, 64, 65, 65, 38, 66, 66, 67, 67, 63, 63, 68, 68, 38, 38, 69, 69, 70, 71, 71, 75, 76, 77, 73, 74, 80, 80, 80, 80, 80, 81, 81, 81, 82, 82, 82, 83, 83, 83, 84, 84, 5, 85, 85, 86, 86, 87, 87, 88, 89, 89, 90, 90, 92, 93, 96, 96, 96, 96, 97, 97, 98, 98, 99, 100, 103, 103, 103, 103, 104, 104, 105, 105, 106, 107 };
  }
}